// pages/studentCourse/studentCourse.js
import api from "../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
      },
      {
        name: "search",
        type: "input",
        label: "会员姓名、手机号",
      },
      {
        name: "course_name",
        type: "input",
        label: "课程名称",
      },
      {
        name: "coach_id",
        type: "pickerCoach",
        label: "教练",
      },
      {
        name: "date",
        names: ["expire_time_form", "expire_start_date", "expire_end_date"],
        type: "pickerDate",
        label: "到期时间",
      },
      {
        name: "course_status",
        type: "picker",
        label: "状态",
        span: 8
      },
      // {
      //   name: "shop_id",
      //   label: "门店",
      //   type: "pickerShop",
      // },
    ],
    searchNum: [
      {
        label: "金额(元)",
        show: true,
        num: "0.00",
      },
      {
        label: "课数",
        num: 0,
      },
      {
        label: "节数",
        num: 0,
      },
      {
        label: "已上课(元)",
        show: true,
        num: 0,
        subhead: "0节",
      },
      {
        label: "未上课(元)",
        show: true,
        num: 0,
        subhead: "0节",
      },
    ],
    sExplain: {
      title: "搜索说明",
      content: "时间可以选择，对会员名称、手机号、课程名称、课程状态、到期时间、教练进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content: "根据搜索条件对课程总金额，课数，节数，已上课金额、节数，未上课金额、节数进行统计",
    },
    searchParam: {
      time_form: "month",
    },
    //#endregion
    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum } = this.data;
    searchParam = {
      ...searchParam
    }
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreCourseList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if(JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)) return
        let data = res.data;
     
        if (page === 1) {
          searchNum[0].num = Number(data.entirety.cost).toFixed(2);
          searchNum[1].num = data.entirety.course_num;
          searchNum[2].num = data.entirety.classes_num;
          searchNum[3].num = Number(data.entirety.ynmt_cost).toFixed(2);
          searchNum[3].subhead = data.entirety.ynmt + '节'
          searchNum[4].num = Number(data.entirety.un_ynmt_cost).toFixed(2);
          searchNum[4].subhead = data.entirety.un_ynmt + '节'
          
          this.setData({
            searchNum,
            'searchParam.shop_id': data.shop_info.shop_id
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          data.list = data.list.map(i => {
            i.user_info.tagVal = 0
            i.user_info.tagStr = '会员'
            i.coach_info.tagVal = 2
            i.coach_info.tagStr = '教练'
            return {
              ...i
            }
          })
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
